//
//  UIView+buttonView.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/2/3.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "UIView+buttonView.h"
#import <objc/runtime.h>


static BOOL isChangeLine;
static CGFloat normalWidth;

@implementation UIView (buttonView)

/**
 Property 'delegate' requires method 'delegate' to be defined - use @dynamic or provide a method implementation in this category
 
 属性“delegate”要求定义方法“delegate”-请使用@dynamic或在此类别中提供方法实现
 
 分类中添加代理方法 需要实现
 */

@dynamic delegate;

- (id)delegate{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setDelegate:(id<UIViewGDButtonDelegate>)delegate{
    return objc_setAssociatedObject(self, @selector(delegate), delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

/**
添加没有图片的按钮数组
*/
- (void)creatNormalButtonWithTitleArray:(NSArray <NSString *>*)titleArray withRect:(nonnull void (^)(CGRect))rect{
    isChangeLine = NO;
    CGFloat pointX = 10.0; //button X坐标
    CGFloat pointY = 10.0; //button Y坐标
    CGFloat padding = 5.0; //button 间距
    CGFloat btnHeight = 30; //button高度
    CGFloat allWidth = GDScreenW - 10;
    UIFont *titleFont = [UIFont systemFontOfSize:15];
    for (int i = 0; i < titleArray.count; i++) {
        CGRect rect = [titleArray[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : titleFont} context:nil];
        CGFloat btnWidth = rect.size.width + 20;
        if (pointX + btnWidth > allWidth) {//换行
            isChangeLine = YES;
            pointX = 10;//X从新开始
            pointY += (btnHeight + padding);//换行后Y+
        }else{
            //此处说明单个或者单行多个，没有换行，使用按钮的实际宽度
            normalWidth = pointX + btnWidth + 10;
        }
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        but.frame = CGRectMake(pointX, pointY, btnWidth, btnHeight);
        but.tag = i + 10;
        [but addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        but.layer.masksToBounds = YES;
        but.layer.cornerRadius = btnHeight/2;
        but.layer.borderWidth = 1;
        but.layer.borderColor = [UIColor magentaColor].CGColor;
        [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [but setTitle:titleArray[i] forState:UIControlStateNormal];
        but.titleLabel.font = titleFont;//一定要一样
        pointX += (btnWidth + padding);//每次X都加上button宽和间距5
        [self addSubview:but];
    }
    //将按钮 最后的 总高度 改变当前View 的高度
    CGRect rect2 = self.frame;
    rect2.size.height = pointY + btnHeight + 10;
    if(isChangeLine == YES){
        rect2.size.width = allWidth;
    }else{
        rect2.size.width = normalWidth;
    }
    self.frame = rect2;
    rect(self.frame);
}

/**
 添加有图片的按钮数组
 */
- (void)creatGDButtonWithArray:(NSArray <GDButton *>*)array{
    CGFloat pointX = 10.0; //button X坐标
    CGFloat pointY = 10.0; //button Y坐标
    CGFloat padding = 5.0; //button 间距
    CGFloat btnHeight = 30; //button高度
    CGFloat allWidth = GDScreenW - 10;
    UIFont *titleFont = [UIFont systemFontOfSize:13];
    for (int i = 0; i < array.count; i++) {
        GDButton *btn = array[i];
        CGFloat btnWidth = btn.frame.size.width + 10;
        //如果按钮超过屏幕宽度换行
        if (pointX + btnWidth > allWidth) {//换行
            pointX = 10;//X从新开始
            pointY += (btnHeight + padding);//换行后Y+
        }
        btn.frame = CGRectMake(pointX, pointY, btnWidth, btnHeight);
        [btn addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = titleFont;//一定要一样
        pointX += (btnWidth + padding);//每次X都加上button宽和间距5
        [self addSubview:btn];
    }
    //将按钮 最后的 总高度 改变当前View 的高度
    CGRect rect2 = self.frame;
    rect2.size.height = pointY + btnHeight + 10;
    self.frame = rect2;
}


- (void)clickButtonAction:(UIButton *)sender{
//    if([self.delegate respondsToSelector:@selector(clickButtonWithTag:)]){
//        [self.delegate clickButtonWithTag:sender.tag];
//    }
    
    if([self.delegate respondsToSelector:@selector(clickButtonWithButton:)]){
        [self.delegate clickButtonWithButton:sender];
    }
    
    
}


@end
